package com.situ.ssh.util;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class JSONUtil {
	   /**
	    * 将任意对象转换为json返回给浏览器
	    *
	    * @param obj
	    * @param excludes 哪些属性不希望转换成json
	    */
	   public static void obj2Json(Object obj, String... excludes) {
	     JsonConfig jsonConfig = new JsonConfig();
	     // 指定哪些属性不进行json转换
	     jsonConfig.setExcludes(excludes);
	     String json = JSONObject.fromObject(obj, jsonConfig).toString();
	     // 返回给浏览器
	     HttpServletResponse response = ServletActionContext.getResponse();
	     response.setContentType("text/json;charset=utf-8");
	     try {
	        response.getWriter().println(json);
	     } catch (IOException e) {
	        e.printStackTrace();
	     }
	   }
	   
	   /*protected void obj2JsonForEasyUI() {
	     obj2Json(pageBean, new String[] { "currentPage", "pageSize", "detachedCriteria" });
	     // obj2Json(pageBean);
	     // obj2Json(pageBean, new String[]{"currentPage"});
	     // obj2Json(pageBean, "currentPage");
	     // obj2Json(pageBean, "currentPage", "pageSize");
	   }*/
	}
